package com.shopoo.oauth.infrastructure.repository;

import java.util.List;

import javax.annotation.Resource;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shopoo.oauth.domain.entity.Menu;
import com.shopoo.oauth.domain.enums.StatusEnum;
import com.shopoo.oauth.domain.repository.MenuRepository;
import com.shopoo.oauth.infrastructure.assembler.InfrastructureAssembler;
import com.shopoo.oauth.infrastructure.converter.InfrastructureConverter;
import com.shopoo.oauth.infrastructure.database.dataobject.OauthMenu;
import com.shopoo.oauth.infrastructure.database.dataobject.OauthRoleMenuR;
import com.shopoo.oauth.infrastructure.database.mapper.MenuMapper;
import com.shopoo.oauth.infrastructure.database.mapper.RoleMenuMapper;
import org.apache.commons.lang3.StringUtils;

import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

/**
 * @Description 菜单数据仓库实现
 * @Date 2020/10/29 4:50 下午
 * @Author <a href="mailto:android_li@sina.cn">Joe</a>
 **/
@Repository
public class MenuRepositoryImpl extends ServiceImpl<MenuMapper, OauthMenu> implements MenuRepository {

    @Resource
    private RoleMenuMapper roleMenuMapper;

    @Resource
    private MenuMapper menuMapper;

    @Override
    public void save(Menu menu) {
        OauthMenu oauthMenu = InfrastructureConverter.INSTANCE.toOauthMenu(menu);
        menu.setStatus(StatusEnum.ENABLE.getValue());
        super.save(oauthMenu);
    }

    @Transactional
    @Override
    public void deleteByMenuId(Long menuId) {
        LambdaQueryWrapper<OauthRoleMenuR> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(OauthRoleMenuR::getMenuId, menuId);
        roleMenuMapper.delete(queryWrapper);
        super.removeById(menuId);
    }

    @Override
    public void deleteByRoleId(Long roleId) {
        LambdaQueryWrapper<OauthRoleMenuR> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(OauthRoleMenuR::getRoleId, roleId);
        roleMenuMapper.delete(queryWrapper);
    }

    @Override
    public List<Menu> findMenuByUserId(String userId) {
        QueryWrapper<OauthMenu> queryWrapper = new QueryWrapper();
        queryWrapper.eq("user_id", userId);
        queryWrapper.orderByAsc("level");
        return menuMapper.findMenuByConditions(queryWrapper);
    }

    @Override
    public List<Menu> findAllValidMenu(Long roleId) {
        return menuMapper.findAllMenu(roleId);
    }

    @Override
    public List<Menu> findByRoleId(Long roleId) {
        return menuMapper.findByRoleId(roleId);
    }

    @Override
    public List<Menu> findAllMenu(Long roleId) {
        return menuMapper.findAllMenu(roleId);
    }

    @Override
    public Menu findById(Long menuId) {
        OauthMenu oauthMenu = super.getById(menuId);
        return InfrastructureConverter.INSTANCE.toMenu(oauthMenu);
    }

    @Override
    public List<Menu> findAllRoleMenus() {
        return roleMenuMapper.findRoleMenus(new LambdaQueryWrapper());
    }

    @Override
    public List<Menu> findByConditions(Menu menu) {
        LambdaQueryWrapper<OauthMenu> queryWrapper = new LambdaQueryWrapper<>();
        if (menu.getMenuId() != null) {
            queryWrapper.eq(OauthMenu::getMenuId, menu.getMenuId());
        }
        if (menu.getParentId() != null) {
            queryWrapper.eq(OauthMenu::getParentId, menu.getParentId());
        }
        if (menu.getStatus() != null) {
            queryWrapper.eq(OauthMenu::getStatus, menu.getStatus());
        }
        if (StringUtils.isNotBlank(menu.getName())) {
            queryWrapper.like(OauthMenu::getName, menu.getName());
        }
        queryWrapper.orderByAsc(OauthMenu::getCreateTime);
        List<OauthMenu> list = super.list(queryWrapper);
        return InfrastructureAssembler.transferMenuList(list);
    }

    @Override
    public List<Menu> findRoleMenus() {
        return null;
    }

    @Override
    public void updateById(Menu menu) {
        OauthMenu oauthMenu = InfrastructureConverter.INSTANCE.toOauthMenu(menu);
        boolean flag = super.updateById(oauthMenu);
        if (!flag) {
            throw new IllegalArgumentException("找不到菜单【"+oauthMenu.getMenuId()+"】信息");
        }
    }
}
